package com.example.petshopping.ui.address.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.address.model.Address;

import java.util.ArrayList;
import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder> {

    private Context context;
    private List<Address> addressList;
    private OnAddressActionListener listener;

    public AddressAdapter(Context context, List<Address> addressList) {
        this.context = context;
        this.addressList = new ArrayList<>(addressList);
    }

    public void setOnAddressActionListener(OnAddressActionListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public AddressViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_address, parent, false);
        return new AddressViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AddressViewHolder holder, int position) {
        Address address = addressList.get(position);

        holder.tvContactInfo.setText(address.getContactInfo());
        holder.tvAddressDetail.setText(address.getFullAddress());
        holder.cbDefault.setChecked(address.isDefault());

        if (address.isDefault()) {
            holder.tvDefaultText.setText("已设为默认");
        } else {
            holder.tvDefaultText.setText("默认");
        }

        // 删除按钮点击事件
        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeleteClick(address);
            }
        });

        // 编辑按钮点击事件
        holder.btnEdit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEditClick(address);
            }
        });

        // 复制按钮点击事件
        holder.btnCopy.setOnClickListener(v -> {
            if (listener != null) {
                listener.onCopyClick(address);
            }
        });

        // 默认地址点击事件
        if (!address.isDefault()) {
            holder.layoutDefault.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onSetDefaultClick(address);
                }
            });
        } else {
            holder.layoutDefault.setClickable(false);
        }
    }

    @Override
    public int getItemCount() {
        return addressList.size();
    }

    public void updateData(List<Address> addresses) {
        this.addressList.clear();
        if (addresses != null) {
            this.addressList.addAll(addresses);
        }
        notifyDataSetChanged();
    }

    public void removeAddress(Address address) {
        int position = addressList.indexOf(address);
        if (position != -1) {
            addressList.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void setDefaultAddress(Address address) {
        for (int i = 0; i < addressList.size(); i++) {
            Address item = addressList.get(i);
            boolean wasDefault = item.isDefault();
            item.setDefault(item.equals(address));
            
            if (wasDefault != item.isDefault()) {
                notifyItemChanged(i);
            }
        }
    }

    public static class AddressViewHolder extends RecyclerView.ViewHolder {
        TextView tvContactInfo;
        TextView tvAddressDetail;
        TextView tvDefaultText;
        CheckBox cbDefault;
        ImageView btnDelete;
        LinearLayout btnEdit;
        LinearLayout btnCopy;
        LinearLayout layoutDefault;

        public AddressViewHolder(@NonNull View itemView) {
            super(itemView);
            tvContactInfo = itemView.findViewById(R.id.tv_contact_info);
            tvAddressDetail = itemView.findViewById(R.id.tv_address_detail);
            tvDefaultText = itemView.findViewById(R.id.tv_default_text);
            cbDefault = itemView.findViewById(R.id.cb_default);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnCopy = itemView.findViewById(R.id.btn_copy);
            layoutDefault = itemView.findViewById(R.id.layout_default);
        }
    }

    public interface OnAddressActionListener {
        void onDeleteClick(Address address);
        void onEditClick(Address address);
        void onCopyClick(Address address);
        void onSetDefaultClick(Address address);
    }
} 